﻿using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;

[TestClass]
public class OrderServiceTest
{
    private OrderService _orderService;
    private List<Order> _orders;

    [TestInitialize]
    public void TestInitialize()
    {
        _orderService = new OrderService();
        _orders = new List<Order>
        {
            new Order("order1", "customer1"),
            new Order("order2", "customer2"),
            new Order("order3", "customer1")
        };
    }

    [TestMethod]
    public void TestAddOrder()
    {
        Order order = new Order("order4", "customer2");
        _orderService.AddOrder(order);
        CollectionAssert.Contains(_orderService.Orders, order);
    }

    [TestMethod]
    public void TestRemoveOrder()
    {
        Order order = _orders[0];
        _orderService.RemoveOrder(order);
        CollectionAssert.DoesNotContain(_orderService.Orders, order);
    }

    [TestMethod]
    public void TestUpdateOrder()
    {

        // 创建一个 Order 对象，作为待更新的订单
        var orderToUpdate = new Order("order1", "customer01");

        // 调用 OrderService 的 UpdateOrder 方法，将订单更新到 Orders 列表中
        _orderService.UpdateOrder(orderToUpdate);

        // 使用 Assert 方法来验证更新是否成功
        var updatedOrder = _orderService.GetOrderById("order1");
        Assert.AreEqual(updatedOrder.CustomerName, "customer01");
    }
    [TestMethod]
    public void TestGetOrderById()
    {
        // 初始化 OrderService 和 Orders 列表
        TestInitialize();

        // 创建一个 Order 对象，将其添加到 Orders 列表中
        var orderToAdd = new Order("order4", "customer3");
        _orderService.AddOrder(orderToAdd);

        // 调用 OrderService 的 GetOrderById 方法，通过订单号获取订单
        var retrievedOrder = _orderService.GetOrderById("order4");

        // 使用 Assert 方法来验证获取到的订单是否正确
        Assert.AreEqual(retrievedOrder.CustomerName, "customer3");
    }
    [TestMethod]
    public void TestQueryByProductName()
    {
        // Arrange
        var orderService = new OrderService();
        var orders = new List<Order> {
        new Order{Id = 1, Customer = new Customer("Alice"), Details = new List<OrderDetail>{ new OrderDetail("apple", 2.0, 100), new OrderDetail("banana", 1.0, 200)}},
        new Order{Id = 2, Customer = new Customer("Bob"), Details = new List<OrderDetail>{ new OrderDetail("apple", 1.5, 50), new OrderDetail("pear", 3.0, 20)}}};

        // Act
        var result = orderService.QueryByProductName("apple", orders);

        // Assert
        Assert.AreEqual(2, result.Count);
        Assert.AreEqual(1, result[0].Id);
        Assert.AreEqual(2, result[1].Id);
    }
    [TestMethod]
    public void TestQueryByCustomer()
    {
        // Arrange
        var orderService = new OrderService();
        var customer = new Customer { Name = "John" };
        var order1 = new Order { Customer = customer };
        var order2 = new Order { Customer = new Customer { Name = "Bob" } };
        orderService.AddOrder(order1);
        orderService.AddOrder(order2);

        // Act
        var result = orderService.QueryByCustomer(customer);

        // Assert
        CollectionAssert.Contains(result, order1);
        CollectionAssert.DoesNotContain(result, order2);
    }
    [TestMethod]
    public void TestSortOrders()
    {
        // Arrange
        OrderService orderService = new OrderService();
        Order order1 = new Order(1, "Customer1");
        Order order2 = new Order(2, "Customer2");
        Order order3 = new Order(3, "Customer3");
        orderService.AddOrder(order2);
        orderService.AddOrder(order3);
        orderService.AddOrder(order1);

        // Act
        orderService.SortOrders();

        // Assert
        Assert.AreEqual(order1, orderService.Orders[0]);
        Assert.AreEqual(order2, orderService.Orders[1]);
        Assert.AreEqual(order3, orderService.Orders[2]);
    }
    [TestMethod]
    public void TestImport()
    {
        // Arrange
        OrderService orderService = new OrderService();
        string fileName = "test.xml";
        Order order = new Order() { OrderId = 1, CustomerName = "John Doe", OrderItems = new List<OrderItem>() };
        orderService.AddOrder(order);
        orderService.Export(fileName);

        // Act
        orderService.Import(fileName);

        // Assert
        Assert.AreEqual(1, orderService.Orders.Count);
        Assert.AreEqual(order, orderService.Orders[0]);
    }

}

